package com.epoint.netty.boundhandler;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;

import java.net.SocketAddress;
import java.nio.charset.Charset;

public class Server {

    public static void main(String[] args) {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    // 加载处理器
                    protected void initChannel(NioSocketChannel ch) {
                        // 执行链
                        ChannelPipeline pipeline = ch.pipeline();

                        // 读1
                        pipeline.addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("read 1");
                                // 把当前执行处理过的结果msg 传给下一个read处理器，也就是【读2】
                                ctx.fireChannelRead(msg);
                            }
                        });
                        // 读2
                        pipeline.addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("read 2");
                                ByteBuf buf = (ByteBuf) msg;
                                Person person = new Person(buf.toString(Charset.defaultCharset()));
                                // 把当前执行处理过的结果msg 传给下一个read处理器，也就是【读3】
                                ctx.fireChannelRead(person);
                            }
                        });
                        // 读3
                        pipeline.addLast(new ChannelInboundHandlerAdapter(){
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println("read 3");
                                // 把当前执行处理过的结果msg 传给下一个read处理器，但是在这个执行链上没有下一个read了
                                // 故这里写不写 一个效果
                                ctx.fireChannelRead(msg);
                                // 为了演示效果，这里写一个write事件，然后触发下面的【写处理器】
                                ctx.channel().writeAndFlush(msg);
                            }
                        });

                        /***************************** 写 ****************************/
                        // 写1
                        pipeline.addLast(new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("write 1");
                                super.write(ctx, msg, promise);
                            }
                        });
                        // 写2
                        pipeline.addLast(new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("write 2");
                                super.write(ctx, msg, promise);
                            }
                        });
                        // 写3
                        pipeline.addLast(new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                                System.out.println("write 3：" + msg + "---class：" + msg.getClass());
                                super.write(ctx, msg, promise);
                            }
                        });
                    }
                })
                .bind(8080); // 4
    }

    static class Person{

        private String name;

        public Person() {
        }

        public Person(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}
